﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.Revit;

namespace YJKRevitDimensionText.OneKeyPlanDim.Utility
{
	public class FindFamilySymbol
	{
		private static Family LoadRvtFile(Autodesk.Revit.DB.Document doc, string strFamilyFilePath, string strFamilyName)
		{
			Family family = null;
			if (FindFamilySymbol.FindFamily(strFamilyName, out family, doc))
			{
				return family;
			}
			Document document = doc.Application.OpenDocumentFile(strFamilyFilePath);
			if (!FindFamilySymbol.FindFamily(strFamilyName, out family, document))
			{
				return family;
			}
			List<ElementId> list = new List<ElementId>();
			list.Add(family.Id);
			list.AddRange(family.GetFamilySymbolIds());
			CopyPasteOptions copyPasteOptions = new CopyPasteOptions();
			copyPasteOptions.SetDuplicateTypeNamesHandler(new HideAndAcceptDuplicateTypeNamesHandler());
			IEnumerable<ElementId> enumerable = ElementTransformUtils.CopyElements(document, list, doc, Transform.Identity, copyPasteOptions);
			Family family2 = null;
			foreach (ElementId elementId in enumerable)
			{
				family2 = (doc.GetElement(elementId) as Family);
				if (family2 != null)
				{
					return family2;
				}
			}
			return family2;
		}

		private static DimensionType LoadDimensionTypeFile(Autodesk.Revit.DB.Document doc, string strFamilyFilePath, string strFamilyName)
		{
			DimensionType dimensionType = null;
			if (FindFamilySymbol.FindFamily(strFamilyName, out dimensionType, doc))
			{
				return dimensionType;
			}
			Document document = doc.Application.OpenDocumentFile(strFamilyFilePath);
			if (!FindFamilySymbol.FindFamily(strFamilyName, out dimensionType, document))
			{
				return dimensionType;
			}
			List<ElementId> list = new List<ElementId>();
			list.Add(dimensionType.Id);
			CopyPasteOptions copyPasteOptions = new CopyPasteOptions();
			copyPasteOptions.SetDuplicateTypeNamesHandler(new HideAndAcceptDuplicateTypeNamesHandler());
			ICollection<ElementId> source = ElementTransformUtils.CopyElements(document, list, doc, Transform.Identity, copyPasteOptions);
			return doc.GetElement(source.ElementAt(0)) as DimensionType;
		}

		public static DimensionType GetDimensionTypes(Autodesk.Revit.DB.Document doc, string strFamilyFilePath, string strFamilyName, string strFamilySymbolName)
		{
			DimensionType dimensionType = FindFamilySymbol.LoadDimensionTypeFile(doc, strFamilyFilePath, strFamilySymbolName);
			if (dimensionType == null)
			{
				return null;
			}
			return dimensionType;
		}

		public static FamilySymbol GetFamilySymbol(Autodesk.Revit.DB.Document doc, string strFamilyFilePath, string strFamilyName, string strFamilySymbolName)
		{
			Family family = FindFamilySymbol.LoadRvtFile(doc, strFamilyFilePath, strFamilyName);
			if (family == null)
			{
				return null;
			}
			FamilySymbol familySymbol = FindFamilySymbol.GetFamilySymbol(family, strFamilySymbolName);
			if (familySymbol == null)
			{
				return null;
			}
			UnifiedModified.ActivateFamilySymbol(familySymbol);
			return familySymbol;
		}

		public static Family LoadFamily(string strFamilyName, string strFamilyPath, Autodesk.Revit.DB.Document doc)
		{
			Family result = null;
			if (!FindFamilySymbol.FindFamily(strFamilyName, out result, doc))
			{
				if (!File.Exists(strFamilyPath))
				{
					YJKMessageBox.Show("在路径" + strFamilyPath + "没有找到对应得标注族");
					return result;
				}
                doc.LoadFamily(strFamilyPath, out result);
			}
			return result;
		}

		private static bool FindFamily(string strFamilyName, out DimensionType dimensionType, Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			List<DimensionType> list = filteredElementCollector.OfClass(typeof(DimensionType)).WhereElementIsElementType().Cast<DimensionType>().ToList<DimensionType>();
			if (filteredElementCollector != null)
			{
				filteredElementCollector.ToElements();
				foreach (DimensionType dimensionType2 in list)
				{
					if (dimensionType2.Name == strFamilyName)
					{
						dimensionType = dimensionType2;
						return true;
					}
				}
			}
			dimensionType = null;
			return false;
		}

		private static bool FindFamily(string strFamilyName, out Family family, Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			List<Family> list = filteredElementCollector.OfClass(typeof(Family)).WhereElementIsNotElementType().Cast<Family>().ToList<Family>();
			if (filteredElementCollector != null)
			{
				filteredElementCollector.ToElements();
				foreach (Family family2 in list)
				{
					if (family2.Name == strFamilyName)
					{
						family = family2;
						return true;
					}
				}
			}
			family = null;
			return false;
		}

		private static FamilySymbol GetFamilySymbol(Family family, string strFamilySymbolName)
		{
			if (family == null || "" == strFamilySymbolName)
			{
				return null;
			}
			List<FamilySymbol> symbols = FindFamilySymbol.GetSymbols(family);
			if (0 >= symbols.Count)
			{
				return null;
			}
			FamilySymbol result = symbols[0];
			foreach (FamilySymbol familySymbol in symbols)
			{
				if (familySymbol.Name == strFamilySymbolName)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}

		private static List<FamilySymbol> GetSymbols(Family family)
		{
			List<FamilySymbol> list = new List<FamilySymbol>();
			foreach (ElementId elementId in family.GetFamilySymbolIds())
			{
				FamilySymbol item = family.Document.GetElement(elementId) as FamilySymbol;
				list.Add(item);
			}
			return list;
		}

		private Document m_doc;
	}
}
